﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using InHD.model;
using System.Windows.Input;
using InHD.Properties;
using InHD.dataaccess;
using InHD.commands;
using InHD.utilities;
using System.Windows;
using System.Data;
using InHD.utilities.enums;

namespace InHD.viewmodels
{
    class ProductViewModel : WorkspaceViewModel
    {
        private Product _product;
        private ProductRepository _productRepository;
        private CategoryRepository _categoryRepo;
        private SupplierRepository _supRepo;
        private List<Product> _products;

        private WorkspaceViewModel _pageContent;
        
        public WorkspaceViewModel PageContent
        {
            get
            {
                return this._pageContent;
            }
            set
            {
                if (this._pageContent != value)
                {
                    this._pageContent = value;
                    OnPropertyChanged("PageContent");
                }
            }
        }

        #region Constructor
        public ProductViewModel()
        {
            _productRepository = new ProductRepository();
            _categoryRepo = new CategoryRepository();
            _supRepo = new SupplierRepository();

            _categoryRepo.LoadAllCategories();
            _supRepo.LoadAllSuppliers();

            //// tam thoi cu load het category
            foreach (Category c in this._categoryRepo.GetCategory())
            {
                this._productRepository.LoadProductsByCategory(c.CategoryUNK);
            }
            this._products = this._productRepository.GetLoadedProducts();
            ////


            this.PageContent = new ProductsListViewModel(this._products);
            this.PageContent.RequestClose += this.HandleCloseCurrentPage;
        }
        #endregion

        private int _rowIndex;
        private void HandleCloseCurrentPage(object source, EventArgs e)
        {
            this.PageContent.RequestClose -= this.HandleCloseCurrentPage;
            if (this.PageContent is ProductsListViewModel)
            {
                string commandExcuted = ((ProductsListViewModel)PageContent).commandExcute;
                if (string.IsNullOrEmpty(commandExcuted))
                {
                    OnRequestClose();
                    return;
                }

                if (commandExcuted.Equals("View"))
                {
                    ActionModes mode = ActionModes.Edit;
                    _product = ((ProductsListViewModel)PageContent).SelectedProduct.CreateShallowCopy();
                    this.PageContent = new ProductFormViewModel(mode, _product, this._categoryRepo.GetCategory(), this._supRepo.GetSuppliers());
                    this.PageContent.RequestClose += this.HandleCloseCurrentPage;
                    OnPropertyChanged("PageContent");
                }
                else if (commandExcuted.Equals("Add"))
                {
                    ActionModes mode = ActionModes.New;
                    this._product = Product.CreateProduct();
                    this.PageContent = new ProductFormViewModel(mode, _product, this._categoryRepo.GetCategory(), this._supRepo.GetSuppliers());
                    this.PageContent.RequestClose += this.HandleCloseCurrentPage;
                    OnPropertyChanged("PageContent");
                }
            }
            else if (this.PageContent is ProductFormViewModel)
            {
                string commandExcuted = ((ProductFormViewModel)PageContent).commandExcute;
                if (commandExcuted.Equals("Close"))
                {
                    this._product = null;
                    this.PageContent = new ProductsListViewModel(this._products);
                    this.PageContent.RequestClose += this.HandleCloseCurrentPage;
                    OnPropertyChanged("PageContent");
                }
                else if (commandExcuted.Equals("Save"))
                {
                    if (((ProductFormViewModel)PageContent).FormMode.Equals(ActionModes.New))
                    {
                        string result = this._productRepository.AddProduct(((ProductFormViewModel)PageContent).CurrentProduct);
                        if (string.IsNullOrEmpty(result))
                        {
                            this._products = _productRepository.GetLoadedProducts();
                            this.PageContent = new ProductsListViewModel(this._products);
                        }
                        else if (result.Equals("DuplicateCode"))
                        {
                            MessageBox.Show(Resources.InHD_Error_DuplicateCode);
                            this.PageContent = new ProductFormViewModel(ActionModes.New, ((ProductFormViewModel)PageContent).CurrentProduct, this._categoryRepo.GetCategory(), this._supRepo.GetSuppliers());
                        }
                        else
                        {
                            MessageBox.Show(Resources.InHD_DB_Error);
                            this.PageContent = new ProductFormViewModel(ActionModes.New, ((ProductFormViewModel)PageContent).CurrentProduct, this._categoryRepo.GetCategory(), this._supRepo.GetSuppliers());
                        }
                    }
                    else
                    {
                        string result = this._productRepository.EditProduct(((ProductFormViewModel)PageContent).CurrentProduct);
                        if (string.IsNullOrEmpty(result))
                        {
                            this._products = _productRepository.GetLoadedProducts();
                            this.PageContent = new ProductsListViewModel(this._products);
                        }
                        else if (result.Equals("DuplicateCode"))
                        {
                            MessageBox.Show(Resources.InHD_Error_DuplicateCode);
                            this.PageContent = new ProductFormViewModel(ActionModes.Edit, ((ProductFormViewModel)PageContent).CurrentProduct, this._categoryRepo.GetCategory(), this._supRepo.GetSuppliers());
                        }
                        else
                        {
                            MessageBox.Show(Resources.InHD_DB_Error);
                            this.PageContent = new ProductFormViewModel(ActionModes.Edit, ((ProductFormViewModel)PageContent).CurrentProduct, this._categoryRepo.GetCategory(), this._supRepo.GetSuppliers());
                        }
                    }

                    this.PageContent.RequestClose += this.HandleCloseCurrentPage;
                    OnPropertyChanged("PageContent");
                }
                else if (commandExcuted.Equals("Delete"))
                {
                    if (!string.IsNullOrEmpty(this._productRepository.DeleteProduct(((ProductFormViewModel)PageContent).CurrentProduct.ProductUNK)))
                    {
                        MessageBox.Show(Resources.InHD_DB_Error);
                    }
                    else
                    {
                        this._products = _productRepository.GetLoadedProducts();
                    }

                    this.PageContent = new ProductsListViewModel(this._products);
                    this.PageContent.RequestClose += this.HandleCloseCurrentPage;
                    OnPropertyChanged("PageContent");
                }
            }
        }

        public override string DisplayName
        {
            get
            {
                try
                {
                    return Resources.Title_Product;
                }
                catch (Exception ex)
                {
                    return string.Empty;
                }
            }

        }
    }
}
